/**
 * auto: kaseisai
 * email: 1015064692@qq.com
 */

//蛇头,由四个方向的的蛇头组成，集合成一个数组
snakeImgTop = new Image();
snakeImgTop.src = "images/head_top.gif";
snakeImgRight = new Image();
snakeImgRight.src = "images/head_right.gif";
snakeImgBottom = new Image();
snakeImgBottom.src = "images/head_Bottom.gif";
snakeImgLeft = new Image();
snakeImgLeft.src = "images/head_Left.gif";
snakeImg =new Array(snakeImgTop,snakeImgRight,snakeImgBottom,snakeImgLeft);
//蛇身，都是同一个小球
bodyImg = new Image();
bodyImg.src = "images/body.gif";
//随机块
randSnake = null;
//随机生成障碍物次数
roundSnake = 0;
//蛇类
function snake(x,y,img,direction){
	//蛇的位置
	this.x = x;
	this.y = y;
	//蛇的图片
	this.img = img;
	//蛇的方向
	this.direction = direction;
	//更改蛇的位置
	this.changePos =  function(){
		//除了蛇头外的蛇身处理
		for(var i=(snakeList.length -1); i >=1; i--)
		{
			snakeList[i].x = snakeList[i-1].x;
			snakeList[i].y = snakeList[i-1].y;
		}
		//蛇头有四个方向，需要单独处理,四个方向统一为上右下左
		switch( snakeList[0].direction){
			case 0:
				snakeList[0].y -= 25;
				break;
			case 1:
				snakeList[0].x += 25;
				break;
			case 2:
				snakeList[0].y += 25;
				break;
			case 3:
				snakeList[0].x -= 25;
				break;
		}
	}
		//重绘蛇
		this.draw = function(){
			for(var i = 0; i <snakeList.length;i++){
				//四个参数，分别为：贴图，坐标x,坐标y,width,height
				canvas.drawImage(snakeList[i].img,snakeList[i].x,
					snakeList[i].y,25,25);
			}
		}
		//随机块碰撞检测,检测蛇头是否碰撞到随机块
		this.hitRandSnake = function(){
			var x= snakeList[0].x;
			var y = snakeList[0].y;
			// 判断的根据是，当蛇头的坐标与随机块的坐标一样时，表示蛇头碰撞到了随机块
			if(snakeList[0].x == randSnake.x && snakeList[0].y == randSnake.y)
			{
				// 碰撞时也有四个方向，同样需要进行判断，根据四个方向的碰撞得到新的x,y值
				switch(snakeList[0].direction)
				{
					case 0:
						y= snakeList[snakeList.length-1].y +25;
						break;
					case 1:
						x= snakeList[snakeList.length-1].x -25;
						break;
					case 0:
						y= snakeList[snakeList.length-1].y -25;
						break;	
					case 1:
						y= snakeList[snakeList.length-1].xy+25;
						break;			
				}
				//碰撞后增加一节蛇身
				snakeList[snakeList.length] = new snake(x,y,bodyImg);
				//增加移动速度
				if(speed>=200) speed -=100;
				snakeNums++;
				//删除随机块
				randSnake = null;
			}
			
		}
		//碰撞检测
		this.checkHit = function(){
			if(this.x <=0 || this.x >= width-25 || this.y <=0 || this.y >= height -25)
			{
				game.over();
				// if(confirm("game over!!!再来一次"))
				// {
				// 	//直接使用game.run()是不行的，因为此时蛇头以及蛇身等等有很多状态存在，需要将这些状态都清空才能重新开始而最简单的方法是重新刷新当前页
				// 	game.run();
				// }
			}
		}
		//蛇头碰撞蛇身测试
		this.checkSnake = function(){
			// 蛇头的坐标
			var x= snakeList[0].x;
			var y = snakeList[0].y;
			//蛇身的坐标
			var i = 0;
			for(var i=1 ;i< snakeList.length;i++)
			{
				if(this.x == snakeList[i].x && this.y ==snakeList[i].y)
				{
					game.over();
				}
			}	
		}
		//键盘事件
		this.keydown = function(e){
			switch(e.keyCode){
				//向上键
				case 38:
					snakeList[0].direction =0;
					snakeList[0].img = snakeImg[0];
					break;
				//向右键
				case 39:
					snakeList[0].direction =1;
					snakeList[0].img = snakeImg[1];
					break;
				//向下键
				case 40:
					snakeList[0].direction =2;
					snakeList[0].img = snakeImg[2];
					break;
				//向左键
				case 37:
					snakeList[0].direction =3;
					snakeList[0].img = snakeImg[3];
					break;
			}
		}
		
		//生成随机块
	this.drawRandSnake = function() {
			if (!randSnake) {
				randSnake = this.getRandPox();
			}
			canvas.drawImage(bodyImg, randSnake.x, randSnake.y, 25, 25);
		}
		//随机获得生成位置
	this.getRandPox = function() {
		while(true)
		{
			var x = Math.floor(Math.random() * (width / 25)) * 25-100;
			var y = Math.floor(Math.random() * (height / 25)) * 25-100;
			x=Math.max(100,x);
			y=Math.max(100,y);
			for (var i = 0; i < snakeList.length; i++) {
				//此点有蛇
				if (x == snakeList[i].x && y == snakeList[i].y) {
					roundSnake++;
					continue;
					// this.getRandPox();
				}
				roundSnake = 0;
				return {
					x: x,
					y: y
				};
			}
		}
	}
		
		
		
	
	
	
	
	
}















